package cn.wolfcode.p2p.business.service.impl;
import java.util.Date;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.business.domain.Bid;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.mapper.BidMapper;
import cn.wolfcode.p2p.business.service.IBidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
@Transactional
public class BidServiceImpl implements IBidService {

        @Autowired
        private BidMapper bidMapper;
        @Override
        public BigDecimal getUserBidSumByBidRequestId(Long userId, Long bidRequestId) {
                return bidMapper.getUserBidSumByBidRequestId(userId,bidRequestId);
        }

        /**
         * 生成一个投标记录
         * @param bidRequest
         * @param amount
         * @param bider
         */
        @Override
        public void createBid(BidRequest bidRequest, BigDecimal amount, LoginInfo bider) {
                Bid bid = new Bid();
                bid.setActualRate(bidRequest.getCurrentRate());
                bid.setAvailableAmount(amount);
                bid.setBidRequestId(bidRequest.getId());
                bid.setBidUser(bider);
                bid.setBidTime(new Date());
                bid.setBidRequestTitle(bidRequest.getTitle());
                bid.setBidRequestState(bidRequest.getBidRequestState());
                bidMapper.insert(bid);
        }

        @Override
        public void batchUpdateState(Long bidRequestId, int bidRequestState) {
                bidMapper.batchUpdateState(bidRequestId,bidRequestState);
        }
}
